/*
 * Created on 2005-3-23
 *
 * Copyright: Bluejay Networks Inc. 2005
 *
 * This software is the proprietary information of Bluejay Networks Inc. 
 * 
 * Use is subject to license terms.
 * 
 */
package com.ims.iaa;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.util.List;

import com.ims.iaa.IaaDebug;
import com.ims.iaa.IaaUtil;
import com.ims.iaa.AD.FlashLink;

/**
 * @author ShengMengBai
 *
 * 
 */
public class FlashURLParser {
	static private Thread monitorThread;
	static int serverPort = IaaDebug.ServerPort;
    static String serverName = IaaDebug.ServerHost;
	private static volatile boolean m_conRun = true;
    
    private static int m_iBatchIndex = 1; 
	
	public static void main( String[] args ) {
		try  {
		    Class.forName("com.ims.iaa.IaaDebug");
		} catch (Exception e){}
		startThread();
		System.out.println("Flash URL parser: Enter <EXIT> to end.");
		
		BufferedReader buff = new BufferedReader( new InputStreamReader(System.in));
		String input = "";
		while (!input.toUpperCase().equals("EXIT")) {
		    try {
				monitorThread.interrupt();
		        input = buff.readLine();
		    } catch (IOException ioException) { }
		}
		m_conRun = false;
		monitorThread.interrupt();
		IaaUtil.log( "Exit by user" );
		System.exit(0);
	}
	
	private static void startThread()  {
		monitorThread = new IaaThread("GetFlashURL")  {
		
			public void run()  {
				if(IaaDebug.Debug)log("start thread to parse flash URL.");		
				while (m_conRun)  {
					//wait for a while when Site Crawler print the report.
					try  {
						Thread.sleep(5000);
					} catch (Exception e){}
					SpiderDBConnection cn = IaaUtil.getSpiderDBConnection(serverName, serverPort);
					try{
					    List lst = cn.getUnparsedFlashImp(toString());
					    for(int i = 0; i < lst.size(); i++){
					        FlashLink flash = (FlashLink)lst.get(i);
					        System.out.println("Start parse flash URL, impression ID is: " + flash.getOldImpressionID());
					        if(flash.parseURL()){
						        System.out.println("get flash URL: " + flash.getTargetURLStr());
					        }
					        cn.updateFlashURL(flash, toString());
					    }
					    //if there isn't flash in database, wait.
					    if(lst.size() == 0){
							try  {
								System.out.println("No unparsed flash in database, wait 10 mins, press ENTER to wake up parser.");
								Thread.sleep(600000);
							} catch (InterruptedException e){
								System.out.println("waked up.");
							}
					    }
					}catch(RemoteException e){
					    e.printStackTrace();
						System.out.println("RemoteException press enter to exit.");
						BufferedReader buff = new BufferedReader( new InputStreamReader(System.in));
						String input = "";
						try{
						    input = buff.readLine();
						}catch(IOException ioException){}
						System.exit(3);
					}
				}//end of while
			} // end of run()
			
			private void log(String s)  {
				IaaUtil.print("[Flash URL parser] " + s);
			}
			
		} ; //end of anonymous class
	
		monitorThread.start();
	}
}
